package org.rcy.ruicingmarket.oss.user;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.rcy.framework.cache.redis.CommonCache;

import org.rcy.ruicingmarket.user.BindRolesVo;
import org.rcy.ruicingmarket.user.OssAddUserVo;
import org.rcy.ruicingmarket.user.OssUserLoginVo;
import org.rcy.ruicingmarket.user.OssUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
import org.springframework.transaction.annotation.Transactional;

/**
*
* @author RuicyWu
*@Date 2021-12-22 14:34:38
*/
@Service
public class OssUserService {

	@Autowired
	private OssUserDao userDao;

    @Autowired
	private BCryptPasswordEncoder encoder;

    @Autowired
    private CommonCache commonCache;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;



    /**
 * 根据id查询BmsUser
 * @param id
 * @return BmsUser
 */
   public OssUser findById(Long id){return userDao.get(id);}

/**
 * 根据id删除BmsUser
 * @param id
 */
   @Transactional(rollbackFor = Exception.class)
   public void remove(Long id){userDao.delete(id);}

/**
 * 新增BmsUser
 * @param userVo
 */
   @Transactional(rollbackFor = Exception.class)
   public void add(OssAddUserVo vo)
   {
       OssUser ossUser = convertToOssUser(vo);
       ossUser.setPassword(encoder.encode(ossUser.getPassword()));
       userDao.insertSelective(ossUser);
   }

/**
 * 修改BmsUser
 * @param entity
 */
   @Transactional(rollbackFor = Exception.class)
   public void edit(OssUser entity){
       userDao.update(entity,true);
   }

    /**
     * 根据账号查询后台用户
     * @param account
     * @return
     */
   public OssUserVo findByAccount(String account)
   {
       return convertToBmsUserVo(userDao.findByAccount(account));
   }


    /**
     * 验证后台用户账号和密码
     * @param loginVo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
   public OssUserVo findAdminByAccountAndPassword(OssUserLoginVo loginVo)
   {
       OssUser ossUser = userDao.findByAccount(loginVo.getAccount());
       if(ossUser !=null&&encoder.matches(loginVo.getPassword(), ossUser.getPassword()))
       {
           Date date = new Date();
           ossUser.setLastLoginTime(date);
           edit(ossUser);
           return new OssUserVo(ossUser.getId(), ossUser.getAccount(), ossUser.getCreateTime(), ossUser.getLastLoginTime());
       }else
           {
               return null;
           }
   }

    /**
     * 批量设置角色
     * @param vo
     */
    @Transactional(rollbackFor = Exception.class)
   public void bindRoles(BindRolesVo vo){
       SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
       vo.getRoleIds().stream().forEach(roleId ->{
           userDao.bindRole(vo.getUserId(), roleId);
       });
       sqlSession.commit();
       sqlSession.clearCache();
   }

   private OssUserVo convertToBmsUserVo(OssUser user) {
       OssUserVo ossUserVo = new OssUserVo();
       BeanUtils.copyProperties(user, ossUserVo);
       return ossUserVo;
   }

   private OssUser convertToOssUser(OssUserVo userVo) {
       OssUser ossUser = new OssUser();
       BeanUtils.copyProperties(userVo, ossUser);
       return ossUser;
   }

    private OssUser convertToOssUser(OssAddUserVo vo) {
        OssUser ossUser = new OssUser();
        BeanUtils.copyProperties(vo, ossUser);
        return ossUser;
    }
}
